package com.zk.mall.service.impl;

import com.zk.mall.dao.GoodsDao;
import com.zk.mall.entity.Goods;
import com.zk.mall.service.GoodsService;
import com.zk.mall.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

public class GoodsServiceImpl implements GoodsService {
    SqlSession session = SqlSessionFactoryUtil.getSqlSession();
    GoodsDao gd = session.getMapper(GoodsDao.class);

    @Override
    public boolean addGoods(Goods goods) {
        Boolean flag = false;
        try {
            flag = gd.addGoods(goods);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.commit();
            session.close();
        }
        return flag;
    }

    @Override
    public boolean updateGoods(Goods goods) {
        boolean flag = false;
        try {
            flag = gd.updateGoods(goods);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.commit();
            session.close();
        }
        return flag;
    }

    @Override
    public boolean deleteGoods(int goodsid) {
        boolean flag = false;
        try {
            flag = gd.deleteGoods(goodsid);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.commit();
            session.close();
        }
        return flag;
    }

    @Override
    public List<Goods> showGoods() {
        List<Goods> goodsList = new ArrayList<>();
        try{
            goodsList = gd.showGoods();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return goodsList;
    }
}
